/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "EP3DFileList.h"
#include "EP3DFileSystem.h"

namespace EP3D
{
	FileList::FileList()
	{
	}

	FileList::~FileList(void)
	{
		m_akFiles.Clear();
	}

	uint FileList::GetFileCount() const
	{
		return m_akFiles.GetSize();
	}

	void FileList::Sort()
	{
		m_akFiles.Sort();
	}

	String FileList::GetFileName(uint uiIndex) const
	{
		if (uiIndex > m_akFiles.GetSize()) {
			return String();
		}

		return m_akFiles[uiIndex].Name;
	}

	String FileList::GetFullFileName(uint uiIndex) const
	{
		if (uiIndex >= m_akFiles.GetSize()) {
			return String();
		}

		return m_akFiles[uiIndex].FullName;
	}

	int FileList::Add(const String& FullPath, uint uiSize, bool bIsDirectory, uint uiId)
	{
		FileListEntry entry;
		entry.Size = uiSize;
		entry.Id = uiId;
		entry.Name = FullPath;
		entry.Name.Replace(TEXT('\\'), TEXT('/'));
		entry.IsDirectory = bIsDirectory;

		if (entry.Name.Right(1) == TEXT("/")) {
			entry.IsDirectory = true;
		}

		entry.Name.MakeLower();
		entry.FullName = entry.Name;

		FileSystem fileSystem;
		fileSystem.DeletePathFromFilename(entry.Name);

		m_akFiles.Add(entry);

		return m_akFiles.GetSize() - 1;
	}

	uint FileList::GetId(uint uiIndex) const
	{
		return uiIndex < m_akFiles.GetSize() ? m_akFiles[uiIndex].Id : 0;
	}

	bool FileList::IsDirectory(uint uiIndex) const
	{
		bool ret = false;
		if (uiIndex < m_akFiles.GetSize()) {
			ret = m_akFiles[uiIndex].IsDirectory;
		}

		return ret;
	}

	uint FileList::GetFileSize(uint uiIndex) const
	{
		return uiIndex < m_akFiles.GetSize() ? m_akFiles[uiIndex].Size : 0;
	}

	int FileList::FindFiles(const String& filename, bool bIsFolder) const
	{
		FileListEntry entry;
		entry.FullName = filename;
		entry.IsDirectory = bIsFolder;

		entry.FullName.Replace(TEXT('\\'), TEXT('/'));
		if (entry.Name.Right(1) == TEXT("/")) {
			entry.IsDirectory = true;
		}

		entry.FullName.MakeLower();
		FileSystem fileSystem;
		fileSystem.DeletePathFromFilename(entry.FullName);

		return m_akFiles.BinarySearchIndex(entry);
	}

}



